﻿<UserControl x:Class="ScrumTable.UI.View.WinTouch.UserControls.PieChart.PieChart.Legend"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Controls="clr-namespace:Microsoft.Surface.Presentation.Controls;assembly=Microsoft.Surface.Presentation" xmlns:PieChart="clr-namespace:ScrumTable.UI.View.WinTouch.UserControls.PieChart.PieChart">

    <UserControl.Resources>
        <PieChart:LegendConverter x:Key="legendConverter" />
        <PieChart:ColourConverter x:Key="colourConverter" />

        <Style TargetType="ListBoxItem">
            <!-- override the default template to remove the default styling on the IsSelected property -->
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border x:Name="border" BorderBrush="Transparent" BorderThickness="1">
                            <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="BorderBrush" TargetName="border">
                                    <Setter.Value>
                                        <SolidColorBrush Color="Black"/>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Foreground="Black" TextAlignment="Center" FontSize="20" FontWeight="Bold"
                   Text="{Binding Path=(PieChart:PieChartLayout.LegendTitle), RelativeSource={RelativeSource AncestorType={x:Type PieChart:Legend}}}"/>

        <Controls:SurfaceListBox Grid.Row="1" Background="Transparent"
            HorizontalContentAlignment="Stretch"  BorderThickness="0"
            ItemsSource="{Binding}" 
            Margin="0" Name="legend" SelectionChanged="OnSelectionChanged">
            
            <Controls:SurfaceListBox.ItemTemplate>
                <DataTemplate>
                    <Grid HorizontalAlignment="Stretch" Margin="3">
                        <Grid.Background>
                            <SolidColorBrush Color="White"/>
                        </Grid.Background>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="20" />
                            <ColumnDefinition />
                            <ColumnDefinition Width="30" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                        </Grid.RowDefinitions>

                        <!-- the colour key  -->
                        <Rectangle Grid.Column="0" Width="13" Height="13" Tag="{Binding}" Fill="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource colourConverter}}"/>

                        <!-- the name of this item -->
                        <Grid Grid.Column="1">
                            <TextBlock MaxWidth="90" Margin="3" Text="{Binding Path=Name}"/>
                        </Grid>

                        <!-- the value of the item's property which this pie chart represents -->
                        <TextBlock Grid.Column="2" HorizontalAlignment="Right"  Margin="3" Tag="{Binding}"
                                   Text="{Binding RelativeSource={RelativeSource Self},Converter={StaticResource legendConverter}}"/>
                        
                    </Grid>
                </DataTemplate>
            </Controls:SurfaceListBox.ItemTemplate>
        </Controls:SurfaceListBox>
    </Grid>
</UserControl>
